/*
 * @Date: 2021-11-30 15:42:07
 * @LastEditors: 春贰
 * @Desc: 
 * @LastEditTime: 2022-10-10 16:11:58
 * @FilePath: \web\vite.config.js
 */
import { resolve } from 'path/posix';
import { defineConfig, loadEnv } from 'vite';
import vue from "@vitejs/plugin-vue";
import visualizer from "rollup-plugin-visualizer";
import path from 'path'
const plugins = [vue()];

//分析插件
if (process.env.vis) {
  plugins.push(
    visualizer({
      open: true,
      gzipSize: true,
      brotliSize: true,
    })
  );
}

const config = ({ mode }) => {
  // console.log(loadEnv(mode, process.cwd()).VITE_PORT);
  return defineConfig({
    plugins,
    base: process.env.ELECTRON == "true" ? './' : "",
    server: {
      port: loadEnv(mode, process.cwd()).VITE_PORT
    },
    resolve: {
      extensions: ['.js', '.vue', '.json'],
      alias: {
        "@": path.resolve(__dirname, "src"), 
      },
    },
    // 开启less支持
    css: {
      preprocessorOptions: {
        less: {
          javascriptEnabled: true
        }
      }
    },
    hmr: {
      overlay: true
    },

    build: {
      minify: 'terser',
      chunkSizeWarningLimit: 1500, // 单个文件大小警告
      //清除console
      terserOptions: {
        compress: {
          drop_console: true,
          drop_debugger: true,
        },
      },
      rollupOptions: {
        // output:{
        //   manualChunks(id){ // 分包
        //     if(id.includes('node_modules')){
        //       return id.toString().split('node_modules/')[1].split('/')[0].toString();
        //     }
        //   }
        // }
      }
    },
    optimizeDeps: {
      exclude: [],  //这玩意一定要加，因为x6 是commonjs 
    },
  })
}
export default config 